/*
 * @Author: 杨三炮
 * @Date: 2021-11-19 13:19:39
 * @LastEditors: 杨三炮
 * @LastEditTime: 2021-11-24 15:58:44
 * @FilePath: \node\node\model\admin_mod.js
 */
module.exports = class admin_mod extends require('./model') {

    /**
     * @description: 数据库模糊查询查询 排序分页
     * @param {*} data 传过来的对象
     * @return {*} Promise 
     */
    static getUserByTypeAndCharMod (data) {
        return new Promise((resolve, reject) => {
            let sql = ''
            if (data.type != 'teacher') {
                sql = `SELECT *FROM student as s
                INNER JOIN class as c
                ON s.class_id=c.c_id
                WHERE ${data.CharType} LIKE '%${data.inputText}%' ORDER BY modifytime DESC;`
            } else {
                sql = `SELECT* FROM teacher WHERE ${data.CharType} LIKE '%${data.inputText}%' ;`
            }

            this.query(sql).then(result => {
                resolve(result)
            }).catch(err => {
                reject(err)
            })
        })
    }

    /**
     * @description: 数据库获取type类型的总数量
     * @param {*} type
     * @return {*} Promise
     */
    static getUserByTypeAndCharTotal () {
        return new Promise((resolve, reject) => {
            let sql = `select count(0) as count from student;`
            this.query(sql).then(result => {
                resolve(result[0].count)
            }).catch(err => {
                reject(err)
            })
        })
    }

    /**
     * @description: 发布通知
     * @param {*} title
     * @param {*} classes
     * @return {*}
     */
    static announceMod (title, content) {
        return new Promise((resolve, reject) => {
            let sql = `INSERT INTO notice (title,content,createtime) VALUES(?,?,now())`
            this.query(sql, this.formatParams(title, content)).then(result => {
                resolve(result)
            }).catch(err => {
                reject(err)
            })
        })
    }
    /**
     * @description: 发布通知给哪个班级
     * @param {*} classes
     * @return {*}
     */
    static announceClassMod (class_id, n_id) {
        return new Promise((resolve, reject) => {
            let sql = `INSERT INTO noticeclass (class_id,n_id) VALUES(?,?)`
            this.query(sql, this.formatParams(class_id, n_id)).then(result => {
                resolve(result)
            }).catch(err => {
                reject(err)
            })
        })
    }

    /**
     * @description: 获取所有通知与(分页获取)
     * @param {*} pageNum
     * @param {*} currPage
     * @return {*}
     */
    static getAllNoticeMod () {
        return new Promise((resolve, reject) => {
            let sql = `SELECT *FROM notice ORDER BY createtime DESC ;`
            this.query(sql).then(result => {
                resolve(result)
            }).catch(err => {
                reject(err)
            })
        })
    }

    /**
     * @description: 获取所有通知数量
     * @return {*}
     */
    static getAllNoticeTotalMod () {
        return new Promise((resolve, reject) => {
            let sql = `SELECT count(0) as count FROM notice ;`
            this.query(sql).then(result => {
                resolve(result[0].count)
            }).catch(err => {
                reject(err)
            })
        })
    }

    /**
     * @description: 获取班级
     * @param {*}
     * @return {*}
     */
    static getClassMod () {
        return new Promise((resolve, reject) => {
            let sql = `SELECT * FROM class ;`
            this.query(sql).then(result => {
                resolve(result)
            }).catch(err => {
                reject(err)
            })
        })
    }

    /**
     * @description: 删除通知
     * @param {*} n_id
     * @return {*}
     */
    static delNoticeMod (n_id) {
        return new Promise((resolve, reject) => {
            let sql = `DELETE  FROM notice where n_id=${n_id} ;`
            this.query(sql).then(result => {
                resolve(1)
            }).catch(err => {
                reject(err)
            })
        })
    }

    /**
     * @description: 查询公告详情
     * @param {*} n_id
     * @return {*}
     */
    static NoticeDetailsMod (n_id) {
        return new Promise((resolve, reject) => {
            let sql = `SELECT *FROM notice WHERE n_id=${n_id} ;`
            this.query(sql).then(result => {
                resolve(result)
            }).catch(err => {
                reject(err)
            })
        })
    }

    /**
     * @description: 看公告发给了哪个班级
     * @param {*} n_id
     * @return {*}
     */
    static NoticeDetailsForClassMod (n_id) {
        return new Promise((resolve, reject) => {
            let sql = `SELECT class_id FROM notice as n
                        INNER JOIN  noticeclass as nc
                        ON n.n_id=nc.n_id 
                        WHERE n.n_id=${n_id};`
            this.query(sql).then(result => {
                resolve(result)
            }).catch(err => {
                reject(err)
            })
        })
    }
    /**
     * @description: 统计人数
     * @param {*}
     * @return {*}
     */
    static NoticeDetailsTotalMod (class_id) {
        return new Promise((resolve, reject) => {
            let sql = `SELECT count(1) as count FROM student WHERE class_id=${class_id};`
            this.query(sql).then(result => {
                resolve(result[0].count)
            }).catch(err => {
                reject(err)
            })
        })
    }
    /**
     * @description: 阅读人数
     * @param {*} n_id
     * @return {*}
     */
    static NoticeDetailsReadMod (n_id) {
        return new Promise((resolve, reject) => {
            let sql = "SELECT *FROM student as s INNER JOIN `read` as r ON s.id=r.u_id WHERE r.n_id=?;"
            this.query(sql, this.formatParams(n_id)).then(result => {
                resolve(result)
            }).catch(err => {
                reject(err)
            })
        })
    }

    /**
     * @description: 获取班级
     * @param {*}
     * @return {*}
     */
    static getClassesMod () {
        return new Promise((resolve, reject) => {
            let sql = "SELECT *FROM class;"
            this.query(sql).then(result => {
                resolve(result)
            }).catch(err => {
                reject(err)
            })
        })
    }
    /**
     * @description: 添加班级
     * @param {*} c_id
     * @param {*} classes
     * @return {*}
     */
    static addClassesMod (c_id, classes) {
        return new Promise((resolve, reject) => {
            let sql = "INSERT INTO class VALUES(?,?);"
            this.query(sql, this.formatParams(c_id, classes)).then(result => {
                resolve(result)
            }).catch(err => {
                reject(err)
            })
        })
    }

    /**
     * @description: 搜索班级
     * @param {*} c_id
     * @param {*} classes
     * @return {*}
     */
    static searchClassesMod (c_id, classes) {
        return new Promise((resolve, reject) => {
            let sql = `SELECT *FROM class WHERE c_id LIKE "%${c_id}%" or classes LIKE "%${classes}%";`
            this.query(sql).then(result => {
                resolve(result)
            }).catch(err => {
                reject(err)
            })
        })
    }

    /**
     * @description: 获取假条
     * @param {*} u_id
     * @return {*}
     */
    static getuserLeaveMod (u_id) {
        return new Promise((resolve, reject) => {
            let sql = "SELECT *FROM `leave` WHERE u_id=?; "
            this.query(sql, this.formatParams(u_id)).then(result => {
                resolve(result)
            }).catch(err => {
                reject(err)
            })
        })
    }
    /**
     * @description: 通过审批
     * @param {*} u_id
     * @return {*}
     */
    static setLeaveStateMod (l_id, state) {
        return new Promise((resolve, reject) => {
            let sql = "UPDATE `leave` SET state=? WHERE l_id=?; "
            this.query(sql, this.formatParams(state, l_id)).then(result => {
                resolve(result)
            }).catch(err => {
                reject(err)
            })
        })
    }


}